package com.tuanbiao.presenter;

import android.text.TextUtils;

import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.OnLifecycleEvent;

import com.hzjq.data.entity.CapDelayEntity;
import com.tuanbiao.base.mvp.BasePresenter;
import com.tuanbiao.controllers.PlanController;
import com.tuanbiao.data.callback.OnBaseResultCallback;
import com.tuanbiao.models.PlanModel;

import timber.log.Timber;

/**
 * @author abiao
 * @since 2022/4/18 11:20
 */
public class PlanSettingPresenter extends BasePresenter<PlanModel, PlanController.SettingView> implements PlanController.SettingPresenter {
    public PlanSettingPresenter(PlanModel model, PlanController.SettingView rootView) {
        super(model, rootView);
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_CREATE)
    private void init() {
        mModel.getDelay(new OnBaseResultCallback<CapDelayEntity>() {
            @Override
            public void onSuccess(CapDelayEntity entity) {
                mRootView.showDefaultDelay(String.valueOf(entity.getStartDelay()), String.valueOf(entity.getRowDelay())
                        , String.valueOf(entity.getHoleDelay()));
                mRootView.showKeyDelay(String.valueOf(entity.getDelay1()), String.valueOf(entity.getDelay2()));
            }

            @Override
            public void onFailed(String msg) {

            }
        });

    }

    @Override
    public void setDelay(String startDelay, String rowDelay, String columnDelay, String f1Delay, String f2Delay) {
        Timber.i(TAG + "setDelay#startDelay:" + startDelay + ",rowDelay:" + rowDelay +
                ",columnDelay:" + columnDelay + ",f1Delay:" + f1Delay + ",f2Delay:" + f2Delay);
        if (TextUtils.isEmpty(startDelay)) {
            mRootView.showMessage("请输入起始延时");
            return;
        }
        if (!TextUtils.isDigitsOnly(startDelay)) {
            mRootView.showMessage("请输入整数");
            return;
        }

        long delay = Long.parseLong(startDelay);
        if (delay > getMaxDelay()) {
            mRootView.showMessage("起始延时不能大于" + getMaxDelay() + "ms");
            return;
        }
        if (TextUtils.isEmpty(rowDelay)) {
            mRootView.showMessage("请输入排间延时");
            return;
        }
        if (!TextUtils.isDigitsOnly(rowDelay)) {
            mRootView.showMessage("请输入整数");
            return;
        }
        delay = Long.parseLong(rowDelay);
        if (delay > getMaxDelay()) {
            mRootView.showMessage("排间延时不能大于" + getMaxDelay() + "ms");
            return;
        }
        if (TextUtils.isEmpty(columnDelay)) {
            mRootView.showMessage("请输入孔间延时");
            return;
        }
        if (!TextUtils.isDigitsOnly(columnDelay)) {
            mRootView.showMessage("请输入整数");
            return;
        }
        delay = Long.parseLong(columnDelay);
        if (delay > getMaxDelay()) {
            mRootView.showMessage("孔间延时不能大于" + getMaxDelay() + "ms");
            return;
        }
        if (TextUtils.isEmpty(f1Delay)) {
            mRootView.showMessage("请输入F1延时");
            return;
        }
        if (!TextUtils.isDigitsOnly(f1Delay)) {
            mRootView.showMessage("请输入整数");
            return;
        }
        delay = Long.parseLong(f1Delay);
        if (delay > getMaxDelay()) {
            mRootView.showMessage("F1延时不能大于" + getMaxDelay() + "ms");
            return;
        }
        if (TextUtils.isEmpty(f2Delay)) {
            mRootView.showMessage("请输入F2延时");
            return;
        }
        if (!TextUtils.isDigitsOnly(f2Delay)) {
            mRootView.showMessage("请输入整数");
            return;
        }
        delay = Long.parseLong(f2Delay);
        if (delay > getMaxDelay()) {
            mRootView.showMessage("F2延时不能大于" + getMaxDelay() + "ms");
            return;
        }
        CapDelayEntity entity = new CapDelayEntity();
        entity.setRowDelay(Long.parseLong(rowDelay));
        entity.setHoleDelay(Long.parseLong(columnDelay));
        entity.setDelay1(Long.parseLong(f1Delay));
        entity.setStartDelay(Long.parseLong(startDelay));
        entity.setDelay2(Long.parseLong(f2Delay));
        mModel.setDelay(entity, new OnBaseResultCallback<CapDelayEntity>() {
            @Override
            public void onSuccess(CapDelayEntity entity) {
                mRootView.showMessage("修改成功");
            }

            @Override
            public void onFailed(String msg) {
                mRootView.showMessage(msg);
            }
        });

    }

    @Override
    public long getMaxDelay() {
        return mModel.getMaxDelay();
    }
}
